<?php 
namespace app\Index\controller;

use think\Controller;
class Menu extends Base
{
    public function get_menu($role="")
    {
        $role = strtoupper($role);
        $menus = file_get_contents("../public/static/yiadmin/json/right.js");//菜单映射表
        $menus = json_decode($menus, true);//JSON转ARRAY
        //映射各角色能使用的菜单
        // $role = "GM";//测试专用，限定角色
        $menus = isset($menus[$role]) ? $menus[$role] : [];

        $menu_v = $this -> get_menu_v($menus);
        $menu_h = $this -> get_menu_h($menus);

        $html = array("menu_h"=>$menu_h,"menu_v"=>$menu_v);
        return $html;
    }
    
    //纵向菜单，左边栏
    public function get_menu_v($menus='')
    {
        $menu_v = $menus;
        return $menu_v;
    }

    //横向菜单重组，顶部
    public function get_menu_h($menus='')
    {
        $menu_h_c = [];
        $menu_h_s = [];
        $menu_temp = $this -> set_menu_h($menus);
        foreach ($menu_temp as $key => $value) {
            if($value["type"] == "contrl"){
                array_push($menu_h_c, $value);
            }
            if($value["type"] == "select"){
                array_push($menu_h_s, $value);
            }
        }
        $menu_h = array(   
            "menu_h_c" => $menu_h_c,
            "menu_h_s" => $menu_h_s
        );
        return $menu_h;
    }

    public function set_menu_h($menus='')
    {
        $sub_menu = [];
        foreach ($menus as $key => $value) {
            if(isset($value['sub'])){
                // 有子菜单
                $sub_temp = $this -> set_menu_h($value['sub']);
                foreach ($sub_temp as $k => $v) {
                    array_push($sub_menu, $v);
                }
            }else{
                // 没有子菜单
                array_push($sub_menu, $value);
            }
        }
        return $sub_menu;
    }
}